package id.mankel.commons.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.HashMap;
import java.util.Map;

public class CodeGenerator {

//    private static final String URL = "jdbc:mysql://172.27.130.63:3306/bosch_after_sales?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&allowMultiQueries=true&useCompression=true&serverTimezone=Asia/Shanghai";
//    private static final String USERNAME = "root";
//    private static final String PASSWORD = "Symbio@2020";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/scaffold?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&allowMultiQueries=true&useCompression=true&serverTimezone=Asia/Shanghai";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {

        final String modulePackage = "com.mankel.module";
        final String moduleName = "system";
        final String[] tables = {
                "sys_permission"
        };

        DataSourceConfig.Builder dsConfig = new DataSourceConfig.Builder(URL, USERNAME, PASSWORD);
        dsConfig.typeConvert(new MyMySqlTypeConvert());
        FastAutoGenerator.create(dsConfig)
                .globalConfig(builder -> {
                    builder.author("Mankel Li") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D:\\workspace\\SERVICE_SCAFFOLD\\src\\main\\java") // 指定输出目录
                            .disableOpenDir()
                    ;
                })
                .packageConfig(builder -> {
                    builder.parent(modulePackage) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .entity("model")
                            .serviceImpl("service")
                            .pathInfo(Map.ofEntries(
                                    Map.entry(OutputFile.mapperXml, "D:\\workspace\\SERVICE_SCAFFOLD\\src\\main\\resources\\mapper"),
                                    Map.entry(OutputFile.other, "D:\\workspace\\MANKEL_FRONT_END_UTILS\\scaffold\\src\\views")
                            )); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .entityBuilder()
                            .disableSerialVersionUID()
                            .enableLombok()
                            .superClass("com.mankel.module.BaseModel")
                            .serviceBuilder()
                            .formatServiceImplFileName("%sService")
                            .controllerBuilder()
                            .enableRestStyle()
                    ;
                })
                .templateConfig(builder -> {
                    builder
                            .entity("templates/mp/entity.java")
                            .mapper("templates/mp/mapper.java")
                            .mapperXml("templates/mp/mapper.xml")
                            .serviceImpl("templates/mp/serviceImpl.java")
                            .controller("templates/mp/controller.java")
                    ;
                })
                .injectionConfig(builder -> {
                    Map<String, String> fileMap = new HashMap<>();
                    builder.beforeOutputFile((tableInfo, stringObjectMap) -> {
                        fileMap.clear();
                        fileMap.put(tableInfo.getEntityName() + "PageRequest.java", "templates/mp/pageRequest.java.vm");
                        fileMap.put(tableInfo.getEntityName() + "List.vue", "templates/mp/list.vue.vm");
                        fileMap.put(tableInfo.getEntityName() + "Edit.vue", "templates/mp/edit.vue.vm");
                    });
                    builder.customFile(fileMap);
                })
                .templateEngine(new MyVelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
